package org.one.afternoon.controller;

import org.one.afternoon.common.Result;
import org.one.afternoon.domain.Example;
import org.one.afternoon.service.ExampleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping(value = "/example")
public class ExampleController {
    @Autowired
    private ExampleService exampleService;

    @CrossOrigin // 跨域访问
    @GetMapping("/all")
    public Result<List<Example>> findAllOne(){
        List<Example> exampleList=exampleService.findAll();
        Result<List<Example>> result=Result.success(exampleList);
        return result;
    }


    @CrossOrigin
    @PostMapping("/queryOne")
    @ResponseBody
    public Result<List<Example>> queryOne(@RequestBody Example example) {
        List<Example> exampleList=exampleService.findOne(example);
        Result<List<Example>> result=Result.success(exampleList);
        return result;
    }

    @CrossOrigin
    @PostMapping("/insertOne")
    @ResponseBody
    public Result<Example> addOne(@RequestBody Example example) {
        Example addExample = exampleService.addOne(example);
        Result<Example> result;
        if (addExample == null){
            result = Result.error("添加用户失败");
        }else {
            result = Result.success(addExample);
        }
        return result;
    }

    @CrossOrigin
    @PostMapping("/deleteOne")
    @ResponseBody
    public Result<Integer> deleteOne(@RequestParam Integer index) {
        exampleService.deleteOne(index);
        Result<Integer> result=Result.success(index);
        return result;
    }

    @CrossOrigin
    @PostMapping("/updateOne")
    @ResponseBody
    public Result<Example> updateOne(@RequestBody Example example) {
        Example updateExample = exampleService.updateOne(example);
        Result<Example> result=Result.success(updateExample);
        return result;
    }
}
